<?php
// Start Session
session_start();
require_once('Authorizer.php');
require_once('DBConfigQuery.php');
require_once('GenFunctions.php');
require_once('../config.inc.php');
require_once('../Validation.php');


if (isset($_POST['PaymentBack'])) {
    session_write_close();
    if ( isset($_SESSION['FORMS']['FormPmtAddPmt']))
    {
        unset ($_SESSION['FORMS']['FormPmtAddPmt']);
    }
    if ( isset($_SESSION['FORMS']['FormPmtRemovePmt']))
    {
        unset ($_SESSION['FORMS']['FormPmtRemovePmt']);
    }
    if ( isset($_SESSION['FORMS']['FormPayment']))
    {
        unset ($_SESSION['FORMS']['FormPayment']);
    }
    header('Location: AdminPay.php');
    exit();
}


$q = NULL;
$r = NULL;

foreach ($_SESSION['FORMS']['FormPmtAddPmt'] as $valueNewPmt)
{
    $q = sprintf($U_Paid_Email_PMT, $valueNewPmt);
    $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    if (!$r)
    {// Free DB
        mysqli_close($dbc);
        die("Query failed");
    }
}

foreach ($_SESSION['FORMS']['FormPmtRemovePmt'] as $valueRemovePmt)
{
    $q = sprintf($U_UNPaid_Email_PMT, $valueRemovePmt);
    $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    if (!$r)
    {// Free DB
        mysqli_close($dbc);
        die("Query failed");
    }
}

// Clean DB Connection
if ($r) {
    mysqli_close($dbc);
    session_write_close();
}

// Clean variables
if ( isset($_SESSION['FORMS']['FormPmtAddPmt']))
{
    unset ($_SESSION['FORMS']['FormPmtAddPmt']);
}
if ( isset($_SESSION['FORMS']['FormPmtRemovePmt']))
{
    unset ($_SESSION['FORMS']['FormPmtRemovePmt']);
}
if ( isset($_SESSION['FORMS']['FormPayment']))
{
    unset ($_SESSION['FORMS']['FormPayment']);
}

header("Location: AdminPaySuccess.php");
exit();

?>